package leetcode;

import leetcode.N002AddTwoNumbers.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yinlei
 * @date 2018/7/16
 * @since 1.0.0
 */
public class N019RemoveNthNodeFromEndofList {

    public static void main(String[] args) {

    }
    public static ListNode removeNthFromEnd(ListNode head, int n) {
        List<ListNode> list = new ArrayList<>();
        ListNode node = head;

        do {
            list.add(node);
            node = node.next;
        } while (node != null);

        int l = list.size() - n;

        if (l < 1) {
            return head.next;
        }
        list.get(list.size() - n - 1).next = list.get(list.size() - n).next;
        return head;
    }
}
